import cookie from 'js-cookie';

const LOAD = 'wuyou-pwa/auth/LOAD';
const LOAD_SUCCESS = 'wuyou-pwa/auth/LOAD_SUCCESS';
const LOAD_FAIL = 'wuyou-pwa/auth/LOAD_FAIL';
const LOGIN = 'wuyou-pwa/auth/LOGIN';
const LOGIN_SUCCESS = 'wuyou-pwa/auth/LOGIN_SUCCESS';
const LOGIN_FAIL = 'wuyou-pwa/auth/LOGIN_FAIL';
const LOGOUT = 'wuyou-pwa/auth/LOGOUT';
const LOGOUT_SUCCESS = 'wuyou-pwa/auth/LOGOUT_SUCCESS';
const LOGOUT_FAIL = 'wuyou-pwa/auth/LOGOUT_FAIL';

const initialState = {
  loaded: false,
  memberId: null
};

export default function reducer(state = initialState, action = {}) {
  switch (action.type) {
    case LOAD:
      return {
        ...state,
        loading: true
      };
    case LOAD_SUCCESS:
      return {
        ...state,
        loading: false,
        loaded: true,
        token: action.result.data.token,
        memberId: action.result.data.memberId
      };
    case LOAD_FAIL:
      return {
        ...state,
        loading: false,
        loaded: false,
        error: action.error
      };
    case LOGIN:
      return {
        ...state,
        loggingIn: true
      };
    case LOGIN_SUCCESS:
      return {
        ...state,
        loggingIn: false,
        loaded: true,
        token: action.result.data.token,
        memberId: action.result.data.memberId
      };
    case LOGIN_FAIL:
      return {
        ...state,
        loggingIn: false,
        loginError: action.error
      };
    case LOGOUT:
      return {
        ...state,
        loggingOut: true
      };
    case LOGOUT_SUCCESS:
      return {
        ...state,
        loggingOut: false,
        token: null,
        memberId: null
      };
    case LOGOUT_FAIL:
      return {
        ...state,
        loggingOut: false,
        logoutError: action.error
      };
    default:
      return state;
  }
}

function setCookie({ data }) {
  const options = data.expireTime ? { expires: new Date(data.expireTime * 1000) } : undefined;

  cookie.set('wuyou-jwt', data.token, options);
}

function setToken({ client }) {
  return ({ data }) => {
    const { token } = data;
    client.setJwtToken(token);
  };
}

/*
* Actions
* * * * */

export const isLoaded = globalState => globalState.auth && globalState.auth.loaded;

export function load() {
  return {
    types: [LOAD, LOAD_SUCCESS, LOAD_FAIL],
    promise: async ({ client }) => {
      const response = await client.post('/personsocial/login.do');
      setCookie(response);
      setToken({ client })(response);
      return response;
    }
  };
}

export function login(data) {
  return {
    types: [LOGIN, LOGIN_SUCCESS, LOGIN_FAIL],
    promise: async ({ client }) => {
      try {
        const response = await client.post('/personsocial/login.do', data);
        console.log(response);
        setCookie(response);
        setToken({ client })(response);
        return response;
      } catch (error) {
        throw error;
      }
    }
  };
}

export function logout() {
  return {
    types: [LOGOUT, LOGOUT_SUCCESS, LOGOUT_FAIL],
    promise: async ({ client }) => {
      const response = await client.get('/logout');
      setToken({ client })({ token: null });
      cookie.set('wuyou-jwt', '');
      return response;
    }
  };
}